<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Mutations</title>
</head>

<body>
  <script src="../../node_modules/vue/dist/vue.js"></script>
  <div id="app">
    <counter></counter>
    <counter></counter>
    <counter></counter>
    <button @click="inc">increment</button>
  </div>
  <script>
    // implements this
    function createStore({ state, mutations }) {
      return new Vue({
        data: {
          state
        },
        methods: {
          commit(mutationName, payload) {
            mutations[mutationName](state, payload);
          }
        }
      })
    }

    const store = createStore({
      state: { count: 0 },
      mutations: {
        inc(state, payload) {
          state.count += payload
        }
      }
    })

    const Counter = {
      render(h) {
        return h('div', store.state.count)
      }
    }

    new Vue({
      el: '#app',
      components: { Counter },
      methods: {
        inc() {
          store.commit('inc', 2)
        }
      }
    })
  </script>
</body>

</html>